/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.stat.model.data;

import org.stat.wrapper.Util;

/**
 *
 * @author Susanta Tewari <stewari@yahoo.com>
 */
public class ModelDataSummary {

    /** Field description */
    private final ModelData modelData;

    /**
     * Constructs ...
     *
     * @param modelData description
     */
    public ModelDataSummary(ModelData modelData) {
        this.modelData = modelData;
    }

    /**
     * Find the Maximum of each Request variable.
     * @return
     */
    public double[] getMaxX() {

        boolean[] requestIndices = modelData.requestIndices;
        int xDataLen             = Util.sum(requestIndices);
        double[] results         = new double[xDataLen];
        Record[] records         = modelData.getRecords();

        for (int i = 0; i < records.length; i++) {

            Record record     = records[i];
            Data[] dataPoints = record.getData();
            int count         = 0;

            for (int j = 0; j < dataPoints.length; j++) {

                if (requestIndices[j]) {

                    results[count] = Math.max(results[count], dataPoints[j].getValue());

                    count++;
                }
            }
        }

        return results;
    }

    public double[] getXBar() {

        boolean[] requestIndices = modelData.requestIndices;
        int xDataLen             = Util.sum(requestIndices);
        double[] results         = new double[xDataLen];
        Record[] records         = modelData.getRecords();
        int n                    = records.length;

        for (int i = 0; i < records.length; i++) {

            Record record     = records[i];
            Data[] dataPoints = record.getData();
            int count         = 0;

            for (int j = 0; j < dataPoints.length; j++) {

                if (requestIndices[j]) {

                    results[count] += dataPoints[j].getValue();

                    count++;
                }
            }
        }

        for (int i = 0; i < results.length; i++) {

            results[i] = results[i] / n;
        }

        return results;
    }

    public double[] getYBar() {

        boolean[] responseIndices = modelData.responseIndices;
        int yDataLen              = Util.sum(responseIndices);
        double[] results          = new double[yDataLen];
        Record[] records          = modelData.getRecords();
        int n                     = records.length;

        for (int i = 0; i < records.length; i++) {

            Record record     = records[i];
            Data[] dataPoints = record.getData();
            int count         = 0;

            for (int j = 0; j < dataPoints.length; j++) {

                if (responseIndices[j]) {

                    results[count] += dataPoints[j].getValue();

                    count++;
                }
            }
        }

        for (int i = 0; i < results.length; i++) {

            results[i] = results[i] / n;
        }

        return results;
    }

    public int getSampleSize() {
        return this.modelData.getRecords().length;
    }
}
